package com.stoken.web.control;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.stoken.domain.MemberBaseInfo;
import com.stoken.domain.MemberRegisterInfo;
import com.stoken.service.MemberBaseInfoService;
import com.stoken.service.MemberRegisterInfoService;
import com.stoken.service.MemberShopWalletService;
import com.stoken.service.MemberWalletService;
import com.stoken.service.impl.StokenRule;
import com.stoken.web.StokenUtils;
import com.stoken.web.control.vo.Message;

/**
 * 电子钱包web控制层
 * @author Frzhao
 *
 */
@Controller
@RequestMapping("/mW")
public class MemberWalletControl {
	
	/**
	 * 电子钱包服务层
	 */
	@Autowired
	private MemberWalletService mWalletService;
	
	@Autowired
	private MemberShopWalletService memberShopWalletService;
	
	@Autowired
	private MemberRegisterInfoService memberRegisterInfoService;
	
	@Autowired
    private MemberBaseInfoService memberBaseService;
	/**
	 * 验证余额
	 * @param request
	 * @param modelMpa
	 * @param balance 提现余额
	 * @return “1”表示提现金额大于账户余额，”0“表示成功
	 */
	@RequestMapping("/vftBalance")
	public void verificationBalance(HttpServletRequest request,ModelMap modelMpa,Double balance,String userAccount,HttpServletResponse reponse){
		if(StringUtils.isBlank(userAccount)){
			//获得session里面的user信息
		    MemberBaseInfo  mbi = (MemberBaseInfo) request.getSession().getAttribute(StokenUtils.LOGINACCOUNT);
		    userAccount = mbi.getBaseUsername();
		}		
		String result = mWalletService.verificationBalance(balance, userAccount);
		
		PrintWriter out = null;
		try {
			out = reponse.getWriter();
			if(StringUtils.equals(result, "0")){
				out.print(true);
			}else{
				out.print(false);
			}
			
		} catch (IOException e) {
			out.print(false);
		} finally{
			out.close();
		}
		
	}
	
	@RequestMapping("/vftAccount")
	public void verificationAccount(String userAccount,HttpServletResponse reponse){
		Boolean tag = false;
		
		tag = memberRegisterInfoService.isSystemAccount(userAccount);
		
		PrintWriter out = null;
		try {
			out = reponse.getWriter();
			out.print(tag);
			
		} catch (IOException e) {
			out.print(false);
		} finally{
			out.close();
		}
		
	}
	
	/**
	 * 提现
	 * @param request
	 * @param modelMpa
	 * @param userAccount
	 * @return -1  服务器异常、0 成功、1账户余额不足
	 */
	@RequestMapping("/withdrawals")
	public void withdrawals(HttpServletRequest request,ModelMap modelMpa,String userAccount,Double money,HttpServletResponse reponse){
		if(money < StokenRule.min_APPLYCASH){
			print(reponse,new Message(1,"提现金额最少"+StokenRule.min_APPLYCASH+"元每次"));
			return;
		}
		String result = mWalletService.verificationBalance(money, userAccount);
		if(result.equals("1")){
			print(reponse,new Message(1,"余额不足"));
		}else if(result.equals("0")){
			mWalletService.withdrawals(money, userAccount);
			print(reponse,new Message(0,"提现成功"));
		}else{
			print(reponse,new Message(1,"操作有误"));
		}
		
	}
	
	/**
	 * 现金转账
	 * @param request
	 * @param modelMpa
	 * @param userAccount 操作用户
	 * @param targetAccount 目标账户
	 * @param  money账金额
	 * @return  -1  服务器异常、0 成功、1 账户余额不足
	 */
	@RequestMapping("/transferOfCash")
	public void transferOfCash(HttpServletRequest request,ModelMap modelMpa,String userAccount,String targetAccount, Double cashMoney,HttpServletResponse reponse){
		
		//判断用户余额是否够操作
		String result = mWalletService.verificationBalance(cashMoney, userAccount);   		
		if(result.equals("1")){
			print(reponse,new Message(1,"余额不足"));
		}else if(result.equals("0")){
			mWalletService.transferOfCash(cashMoney, userAccount, targetAccount);
			print(reponse,new Message(0,"转账成功"));
		}else{
			print(reponse,new Message(1,"操作有误"));
		}
		
	}
	
	/**
	 * 会员转账到消费基金
	 * @param request
	 * @param modelMpa
	 * @param userAccount
	 * @param money
	 * @return
	 */
	@RequestMapping("/transferOfConsume")
	public void transferOfConsume(HttpServletRequest request,ModelMap modelMpa,String userCoAccount, Double cMoney,HttpServletResponse reponse){		
		String result = mWalletService.verificationBalance(cMoney, userCoAccount);
		if(result.equals("1")){
			print(reponse,new Message(1,"余额不足"));
		}else if(result.equals("0")){
			mWalletService.transferOfConsume(cMoney, userCoAccount);
			print(reponse,new Message(0,"转账成功"));
		}else{
			print(reponse,new Message(1,"操作有误"));
		}		
		
	}
	
	/**
	 * 输出
	 * @param response
	 * @param message
	 */
	private void print(HttpServletResponse response,Message message){
		try {
			String json = "{\"code\":\""+message.getCode()+"\",\"context\":\""+message.getContext()+"\"}";  
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.write(json);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}	
	
	@RequestMapping("/checkUserAccount")
	public void checkUserAccount(String targetAccount,HttpServletResponse reponse){
	   MemberRegisterInfo meRegInfo = memberRegisterInfoService.findRegisterByUserName(targetAccount);
	   if(meRegInfo == null || (meRegInfo.getRegisterAvtivestate() == 0)){
		   print(reponse,new Message(1,"该用户不存在或未被激活"));
	   }else{
		   MemberBaseInfo mBs = memberBaseService.getMemberBaseInfoByAccount(targetAccount);
		   print(reponse,new Message(1,"姓名："+mBs.getBaseName()));
	   }
	  
	}

	
	
}
